package pap.gui;

import java.awt.event.*;
import javax.swing.*;

import pap.TimesRegister;

public class UserEventListener extends JFrame implements ActionListener {

	private static final long serialVersionUID = 711104987962602240L;
	private JButton btnStart;
	private JButton btnStop;
	private JButton btnPause;
	private JButton btnNext;
	private GUIManager manager;
	TimesRegister timesRegister;

	/**
	 * Control panel initialization for the GUI. It has four buttons: start,
	 * stop , pause and do single step.
	 * 
	 * @param manager
	 * @param timesRegister
	 */
	public UserEventListener(GUIManager manager, TimesRegister timesRegister) {

		this.timesRegister = timesRegister;
		this.manager = manager;

		setTitle("Control Panel");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(385, 90);
		getContentPane().setLayout(null);
		setResizable(false);

		btnStart = new JButton(new ImageIcon("img/play.gif"));
		btnStart.setBounds(20, 20, 70, 25);
		getContentPane().add(btnStart);
		btnStart.addActionListener(this);

		btnStop = new JButton(new ImageIcon("img/stop.gif"));
		btnStop.setBounds(110, 20, 70, 25);
		getContentPane().add(btnStop);
		btnStop.addActionListener(this);

		btnPause = new JButton(new ImageIcon("img/pause.gif"));
		btnPause.setBounds(200, 20, 70, 25);
		getContentPane().add(btnPause);
		btnPause.addActionListener(this);

		btnNext = new JButton(new ImageIcon("img/next.gif"));
		btnNext.setBounds(290, 20, 70, 25);
		getContentPane().add(btnNext);
		btnNext.addActionListener(this);
	}

	public void start() {
		manager.resumeGUI();
	}

	public void stop() {
		manager.stopGUI();
	}

	public void pause() {
		manager.suspendGUI();
	}

	public void nextStep() throws InterruptedException {
		manager.drawStep();
	}

	@Override
	public void actionPerformed(ActionEvent ev) {
		Object src = ev.getSource();

		if (src == btnStart) {
			start();
		} else if (src == btnStop) {
			stop();
		} else if (src == btnPause) {
			pause();
		} else {
			try {
				nextStep();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

}